package cn.edu.njust.alg.leetcode;

import java.util.Stack;

/**
 * 创建时间：2015年6月15日 上午11:47:37 项目名称：AlgorithmExercise
 * 
 * @author Zhou Xiumin
 * @version 1.0
 * @since JDK 1.7.0_21 文件名称：Leetcode_0_t.java 类说明：
 */
public class Leetcode_150_Evaluate_Reverse_Polish_Notation {

	public static int evalRPN(String[] tokens) {
		Stack<Integer> stack = new Stack<Integer>();
		int op1, op2;
		for (int i = 0; i < tokens.length; i++) {
			switch (tokens[i]) {
			case "+":
				op1 = stack.pop();
				op2 = stack.pop();
				stack.push(op1 + op2);
				break;
			case "-":
				op1 = stack.pop();
				op2 = stack.pop();
				stack.push(op2 - op1);
				break;
			case "*":
				op1 = stack.pop();
				op2 = stack.pop();
				stack.push(op1 * op2);
				break;
			case "/":
				op1 = stack.pop();
				op2 = stack.pop();
				stack.push(op2 / op1);
				break;
			default:
				stack.push(Integer.parseInt(tokens[i]));
				break;
			}

		}

		return stack.pop();
	}

	public static void main(String[] args) {
		String[] tokens = { "4", "13", "5", "/", "+" };
		System.out.println(evalRPN(tokens));
	}

}
